package org.eenie.commonlib.ui.bento

import android.view.View
import android.widget.ImageView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout

import cn.bingoogolapple.bgabanner.BGABanner
import com.alibaba.android.arouter.facade.annotation.Route
import com.eenie.common.base.BaseActivity
import com.yelp.android.bento.componentcontrollers.RecyclerViewComponentController
import com.yelp.android.bento.components.ListComponent

import org.eenie.commonlib.R
import org.eenie.commonlib.databinding.ActivityBentoBinding
import org.eenie.commonlib.ui.bento.components.banner.BannerComponent
import org.eenie.commonlib.ui.bento.components.banner.BannerData
import org.eenie.commonlib.ui.bento.components.banner.IBannerData
import org.eenie.commonlib.ui.bento.components.course.CourseItemHolder

@Route(path = BentoActivity.PATH)
class BentoActivity : BaseActivity(), SwipeRefreshLayout.OnRefreshListener, BGABanner.Delegate<ImageView, IBannerData> {


    private lateinit var mBinding: ActivityBentoBinding


    override fun onBannerItemClick(banner: BGABanner?, itemView: ImageView?, model: IBannerData?, position: Int) {
        println("onBannerItemClick")
    }

    private val componentController by lazy {
        RecyclerViewComponentController(mBinding.rvContainer)
    }

    private val mImgs by lazy {

        ArrayList<BannerData>().apply {
            addAll(arrayOf(
                    "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1566288482896&di=4fdbc3c7c2a32ba76050d1888b9d86eb&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201804%2F28%2F20180428121410_idkhm.jpg",
                    "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1566288482894&di=aa398c749306ad06c7585524741f1e3d&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201808%2F13%2F20180813083446_vP8PM.jpeg"
            )
                    .map { BannerData(it) }
                    .toList())
        }


    }

    private val mCourses: ArrayList<String> = ArrayList()

    val mListAdapter by lazy {
        ListComponent(null, CourseItemHolder::class.java).apply {
            setData(mCourses)
            toggleDivider(false)
        }
    }


    companion object {
        const val PATH: String = "/main/bento_page"
    }

    override fun initContentView(): Int {
        return R.layout.activity_bento
    }

    override fun initInjector() {
        for (item in 1..5) {
            mCourses.add("No.$item")
        }

    }


    override fun initUiAndListener(view: View) {
        mBinding = ActivityBentoBinding.bind(view)

        componentController.addComponent(BannerComponent(this, mImgs))

        componentController.addComponent(mListAdapter)


        mBinding.laySwiper.setOnRefreshListener(this)


    }


    override fun onRefresh() {
        mBinding.laySwiper.isRefreshing = false

        (componentController[1] as ListComponent<Nothing, String>).appendData((1..3).map { "List new element $it" })
    }


}
